<?php

namespace App\Http\Controllers\Admin;

use App\Services\ProductCategoryService;
use App\Http\Requests\Admin\ProductCategoryRequest;
use App\Http\Requests;
use App\Http\Controllers\Controller;

class ProductCategoryController extends Controller
{

    private $productCategoryService;

    public function __construct(ProductCategoryService $productCategoryService)
    {
        $this->productCategoryService = $productCategoryService;
    }

    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $treeArray = $this->productCategoryService->getTreeProductCategory();
        return view('admin.product_categories.index', ['treeArray' => $treeArray]);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        $treeArray = $this->productCategoryService->getTreeProductCategory();
        return view('admin.product_categories.create', ['treeArray' => $treeArray]);
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \App\Http\Requests\Admin\ProductCategoryRequest $request
     * @return \Illuminate\Http\Response
     */
    public function store(ProductCategoryRequest $request)
    {
        $this->productCategoryService->saveProductCategory($request->all());
        return redirect()->route('admin.product_categories.index');
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        $productCategory = $this->productCategoryService->getProductCategoryById($id);
        $treeArray = $this->productCategoryService->getTreeProductCategory();
        $subIds = $this->productCategoryService->getAllSubIds($id);
        return view('admin.product_categories.edit',
            ['productCategory' => $productCategory, 'treeArray' => $treeArray, 'subIds' => $subIds]);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \App\Http\Requests\Admin\ProductCategoryRequest $request
     * @param  int $id
     * @return \Illuminate\Http\Response
     */
    public function update(ProductCategoryRequest $request, $id)
    {
        $this->productCategoryService->updateProductCategoryById($id, $request->all());
        return redirect()->route('admin.product_categories.index');
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        $this->productCategoryService->deleteProductCategoryById($id);
        return redirect()->route('admin.product_categories.index');
    }
}
